<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<style>
    .el-input-item {
        width: 260px;
    }

</style>
<div id="app">
    <template>
        <el-form :model="formData" ref="formData" :rules="rules" label-width="100px"
                 style="margin-top: 5%;display: flex;flex-direction: row;align-items: center;flex-wrap: wrap;justify-content: space-between;margin-right: 5%">
                                                                                                                                                                                <el-form-item label="名称" prop="name">
                                <el-input class="el-input-item" size="small" v-model="formData.name"
                                          placeholder="请输入名称"/>
                            </el-form-item>
                                                                                                                                                                                                                <el-form-item label="图片" prop="picture">
                                <div style="display: flex;flex-direction: row;align-items: center">
                                    <el-upload
                                            action="/common/upload/file"
                                            :on-success="handleSuccesspicture"
                                            list-type="picture-card"
                                            :on-error="handleErrorpicture"
                                            :limit="3"
                                            :file-list="defaultpictureList"
                                            :before-upload="beforeUploadpicture"
                                            style="margin-left: 10px"
                                    >
                                        <i class="el-icon-plus"></i>
                                    </el-upload>
                                </div>

                            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                            </el-form>
        <div style="text-align: center;margin-top: 5%">
            <el-button size="small" @click="closePage">取 消</el-button>
            <el-button size="small" type="primary" @click="handleSubmit">确 定</el-button>
        </div>
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/admin/apTest.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    let id = getQueryString("id");
    new Vue({
        el: '#app',
        data() {
            return {
                dialogVisible: false,
                formData: {},
                                                            defaultpictureList:[],
                                                                                                    rules: {
                                                createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
                                        updateTime: [
            {required: true, message: "修改时间不能为空", trigger: "blur"}
        ],
                        }
            }
        },
        created() {
            if (id) {
                detail(id).then((res) => {
                    if (res.code === '200') {
                        this.formData = res.data
                    let pictureArr=[]
            pictureArr.push(res.data.picture)
        this.defaultpictureList =pictureArr.map(item=>{
            return {
                name: item,
                url: item
            }
        });
                                            }
                });
            } else {
                this.formData = {}
            }
                                
        },
        methods: {


            handleSubmit() {
                if (id) {
                    addOrUpdate('/admin/apTest/update', this);
                } else {
                    addOrUpdate('/admin/apTest/add', this);
                }
            },

                    handleSuccesspicture(response, file, fileList) {
            console.log('上传成功', response, file, fileList);
            if(response.code==='200'){
                this.formData.picture = response.data
            }else{
                this.$message.error('删除失败！');
            }
        },
        handleErrorpicture(err, file, fileList) {
            // 文件上传失败的回调函数
            console.log('上传失败', err, file, fileList);
        },
        beforeUploadpicture(file) {
            // 在文件上传之前的钩子函数，可以在这里进行文件格式、大小等的校验
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt500KB = file.size / 1024 <= 500;
            if (!isJPG) {
                this.$message.error('只能上传jpg/png文件!');
            }
            if (!isLt500KB) {
                this.$message.error('文件大小不能超过500KB!');
            }
            return isJPG && isLt500KB;
        },
                                }
    });
</script>
</html>
